use std::io;

use crate::gen_title;

pub fn ch2_12_1() {
    gen_title("ch2_12_1", 18);

    let a = [9, 8, 7, 6, 5];

    let first = a[0];
    let second = a[1];

    println!("first: {}, second: {}", first, second);
}

pub fn ch2_12_2() {
    gen_title("ch2_12_2", 18);

    let a = [1, 2, 3, 4, 5];

    println!("Please enter an array index");

    let mut index = String::new();
    io::stdin()
        .read_line(&mut index)
        .expect("Failed to read line");

    if index.trim().is_empty() {
        index = "0".to_string();
    }

    let index: usize = index
        .trim()
        .parse()
        .expect("Index entered was not a number");

    let element = a[index];

    println!(
        "The value of the element at index {} is: {}",
        index, element
    );
}

pub fn ch2_12_3() {
    gen_title("ch2_12_3", 18);

    let str_array = [
        "rust is good!".to_string(),
        "rust is good!".to_string(),
        "rust is good!".to_string(),
    ];
    println!("{:#?}", str_array);

    let str_array: [String; 8] = std::array::from_fn(|_i| String::from("rust is good!"));
    println!("{:#?}", str_array);
}

pub fn ch2_12_4() {
    gen_title("ch2_12_4", 18);

    let a: [i32; 5] = [1, 2, 3, 4, 5];
    let slice: &[i32] = &a[1..3];
    assert_eq!(slice, &[2, 3]);

    println!("slice: {:?}", slice);
}

pub fn ch2_12_5() {
    gen_title("ch2_12_5", 18);

    let one = [1, 2, 3];
    let two: [u8; 3] = [1, 2, 3];
    let blank1 = [0; 3];
    let blank2 = [0; 3];

    let arrays: [[u8; 3]; 4] = [one, two, blank1, blank2];

    for a in &arrays {
        print!("{:?}", a);

        for n in a.iter() {
            print!("\t{} + 10 = {}", n, n + 10);
        }

        let mut sum = 0;
        for i in 0..a.len() {
            sum += a[i]
        }
        println!("\t({:?} = {})", a, sum);
    }

    println!("arrays: {:?}", arrays);
}

pub fn ch2_12_pra_1() {
    gen_title("ch2_12_pra_1", 18);

    let arr: [usize; 5] = [1, 2, 3, 4, 5];
    println!("arr: {:?}", arr);
    assert_eq!(5, arr.len());
}

pub fn ch2_12_pra_2() {
    gen_title("ch2_12_pra_2", 18);

    let arr0 = [1, 2, 3];
    let arr: [char; 3] = ['a', 'b', 'c'];

    assert_eq!(12, std::mem::size_of_val(&arr));
    println!("arr: {:?}", arr);
    println!("arr0: {:?}", arr0);
    println!("size of arr0: {}", std::mem::size_of_val(&arr0));
    println!("size of arr: {}", std::mem::size_of_val(&arr));
}

pub fn ch2_12_pra_3() {
    gen_title("ch2_12_pra_3", 18);

    let list: [i32; 100] = [1; 100];

    assert_eq!(1, list[0]);
    assert_eq!(100, list.len());
    println!("list: {:?}", list);
}

pub fn ch2_12_pra_4() {
    gen_title("ch2_12_pra_4", 18);

    let arr = [1, 2, 3];
    let s1: &[i32] = &arr[0..2];
    let s2: &str = "hello,world";

    println!("s1: {:?}", s1);
    println!("s2: {:?}", s2);
}

pub fn ch2_12_pra_5() {
    gen_title("ch2_12_pra_5", 18);

    let arr: [char; 3] = ['中', '国', '人'];
    let slice = &arr[..2];

    assert_eq!(16, std::mem::size_of_val(&slice));

    println!("slice: {:?}", slice);
}

pub fn ch2_12_pra_6() {
    gen_title("ch2_12_pra_6", 18);

    let arr: [i32; 5] = [1, 2, 3, 4, 5];
    let slice: &[i32] = &arr[1..=3];
    assert_eq!(slice, &[2, 3, 4]);
    println!("slice: {:?}", slice);
}

pub fn ch2_12_pra_7() {
    gen_title("ch2_12_pra_7", 18);

    let s = String::from("hello");
    let slice1 = &s[0..2];
    let slice2 = &s[0..=1];

    assert_eq!(slice1, slice2);

    println!("slice1: {:?}", slice1);
    println!("slice2: {:?}", slice2);
}

pub fn ch2_12_pra_8() {
    gen_title("ch2_12_pra_8", 18);

    let s = "你好, 世界";
    let slice = &s[0..3];

    assert_eq!(slice, "你");

    println!("slice: {:?}", slice);
}

pub fn ch2_12_pra_9() {
    gen_title("ch2_12_pra_9", 18);

    let mut s = String::from("hello world");

    let ch = first_character(&s);

    println!("the first character is: {}", ch);

    s.clear();

    fn first_character(s: &str) -> &str {
        &s[..1]
    }
}
